<template>
    <div>
        <!--<el-button @click="diagnosisRequest"></el-button>-->
        <el-row>
            <el-tabs v-loading="isLoading" v-model="typeActiveTab" type="card">
                <el-tab-pane label="基本信息" name="tabBase">
                    <base-info v-if="!isLoading" ref="baseInfoForm"
                               :diagnosis="diagnosis">
                    </base-info>
                </el-tab-pane>
                <el-tab-pane label="病理信息" name="tabPathologyInfo">
                    <pathology-info v-if="!isLoading" :diagnosis="diagnosis"></pathology-info>
                </el-tab-pane>
                <el-tab-pane label="病理切片" name="tabPathologyList">
                    <pathology-list v-if="!isLoading" :diagnosis="diagnosis"></pathology-list>
                </el-tab-pane>
                <el-tab-pane label="诊断意见" name="tabOpinion">
                    <opinion v-if="!isLoading" :diagnosis="diagnosis"
                             @diagnosisRequest="diagnosisRequest"
                             @diagnosisSearch="diagnosisSearch"></opinion>
                </el-tab-pane>
                <el-tab-pane label="附件资料" name="tabAttachment">
                    <attachment v-if="!isLoading" :diagnosis="diagnosis"
                                :diagnosis-id="diagnosis.id"></attachment>
                </el-tab-pane>
            </el-tabs>
        </el-row>
    </div>
</template>

<script>
import _ from 'lodash';
import diagnosisDefault from '@templates/diagnosisDefault';
import BaseInfo from './DiagnosisDetailBase';
import PathologyInfo from './DiagnosisDetailPathologyInfo';
import PathologyList from './DiagnosisDetailPathologyList';
import Opinion from './DiagnosisDetailOpinionUnchanged';
import Attachment from './DiagnosisDetailAttachment';

export default {
  name: 'DiagnosisDetailUnchanged',

  components: {
    BaseInfo,
    PathologyInfo,
    PathologyList,
    Opinion,
    Attachment,
  },

  data() {
    return {
      diagnosis: diagnosisDefault,
      typeActiveTab: 'tabBase',
      isLoading: true,
    };
  },
  async mounted() {
    await this.diagnosisRequest();
  },
  methods: {
    async diagnosisRequest() {
      this.isLoading = true;

      const { state } = this.$store;
      const { pathologyId } = state.diagnosis;
      const res = await this.$axios({
        method: 'GET',
        url: '/PROXY-Remote/diagnosis/single',
        params: { pathologyId },
      });

      if (res && res.data.code !== 200) {
        throw new Error(res.data);
      }

      const result = res.data.data;
      this.$store.commit('setPropertyGeneral', {
        key: 'diagnosis',
        value: {
          id: result.id,
          pathologyId,
        },
      });
      result.cardId = Base64.decode(result.cardId);
      Object.keys(this.diagnosis).forEach((key) => {
        if (typeof (this.diagnosis[key]) === 'string') {
          this.diagnosis[key] = _.get(result, key, '');
        } else if (typeof (this.diagnosis[key]) === 'number') {
          this.diagnosis[key] = _.get(result, key, 0);
        } else if (this.diagnosis[key] instanceof Array) {
          this.diagnosis[key] = _.clone(result[key]);
        } else if (this.diagnosis[key] instanceof Object) {
          this.diagnosis[key] = _.get(result, key, { name: '' });
        } else {
          this.diagnosis[key] = _.get(result, key, '');
        }

        if (this.diagnosis[key] === null) {
          this.diagnosis[key] = '';
        }
      });

      // 取材脏器名字获取
      this.diagnosis.sampleOrganName = _.get(this.diagnosis.sampleOrganOrgan, 'name', '');
      this.diagnosis.age = `${this.diagnosis.age}${this.diagnosis.ageUnitName}`;
      this.diagnosis.diagnosisDetailList.forEach((t, index) => {
        if (t.detailString) {
          const detailString = t.detailString.replace('\\', '');
          this.diagnosis.diagnosisDetailList[index].detailJson = JSON.parse(detailString);
        } else {
          this.diagnosis.diagnosisDetailList[index].detailJson = {};
        }
      });

      this.isLoading = false;
    },

    diagnosisSearch(pathologyId) {
      console.log('diagnosisSearch', pathologyId);
      this.$store.commit('setPropertyGeneral', {
        key: 'diagnosis',
        value: {
          pathologyId,
        },
      });
      this.diagnosisRequest();
    },
  },
};

</script>

<style scoped>

</style>
